package il.ac.tau.cg.ex3.entities.scene.lights;

import java.util.Iterator;

import il.ac.tau.cg.ex3.entities.styles.Color;
import il.ac.tau.cg.ex3.math.Point;
import il.ac.tau.cg.ex3.math.shapes.Rectangle;

public class AreaLight extends LightGroup
{	
	private PointLight[][] lightsGrid;
	private int dimension;

	public AreaLight(Color color, int dimension, Point p0, Point p1, Point p2)
	{
		super(color);
		
		this.dimension = dimension;
		this.lightsGrid = new PointLight[dimension][dimension];
		
		Rectangle rect = new Rectangle(p0, p1, p2);
		
		Point[][] pointsGrid = rect.createGrid(dimension);
		
		Color lightColor = new Color(color.getR() / (dimension * dimension), 
								     color.getG() / (dimension * dimension), 
								     color.getB() / (dimension * dimension)); 
			
		for (int i = 0; i < dimension; i++)
		{
			for (int j = 0; j < dimension; j++)
			{
				this.lightsGrid[i][j] = new PointLight(lightColor.clone(), pointsGrid[i][j]);
			}
		}
	}
	
	
	@Override
	public Iterator<Light> iterator()
	{
		return new LightsIterator();
	}
	
	public class LightsIterator implements Iterator<Light>
	{
		private int counter;
		
		public LightsIterator()
		{
			counter = 0;
		}
		
		@Override
		public boolean hasNext()
		{
			return (counter < dimension * dimension);
		}

		@Override
		public Light next()
		{
			int i = counter / dimension;
			int j = counter % dimension;
			
			counter++;
			
			return lightsGrid[i][j];
		}

		@Override
		public void remove()
		{
			throw new UnsupportedOperationException();
		}
	}
}
